from data_define import Record
import json


class ReadFile:
    def read_data(self):
        pass


class TextFileRead(ReadFile):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        record_list = []
        with open(self.path, "r", encoding="UTF-8") as f:
            for line in f.readlines():
                line = line.strip()
                line_data = line.split(",")
                params = {"date": line_data[0], "order_id": line_data[1], "money": line_data[2],
                          "province": line_data[3]}
                # print(params)
                re = Record(params)
                record_list.append(re)

            return record_list


class JsonFileRead(ReadFile):
    def __init__(self, path):
        self.path = path

    def read_data(self):
        record_list = []
        with open(self.path, "r", encoding="UTF-8") as f:
            for line in f.readlines():
                line = json.loads(line.strip())
                print(line)
                params = {"date": line['date'], "order_id": line['order_id'], "money": line['money'],
                          "province": line['province']}
                re = Record(params)
                record_list.append(re)

            return record_list


if __name__ == "__main__":
    readText = TextFileRead("2011年1月销售数据.txt")
    # print()
    list1 = readText.read_data()
    for l in list1:
        print(l.date)
    readJson = JsonFileRead("2011年2月销售数据JSON.txt")
    # print(readJson.read_data())
